package edu.fhsu.csci468;

import javax.swing.event.EventListenerList;

public class HandleEvents implements ISensorEventListener, 
									 IControlPanelEventListener
{

	private ESecurityLevel level = ESecurityLevel.Off;
	
	private EventListenerList listenerList = new EventListenerList();
	//private AlarmManager alarmManager = new AlarmManager(this);
	
	public void sensorEventOccurred(SensorEvent evt) 
	{
		if(level == ESecurityLevel.Off)
		{
			if(evt.getSensorType() == SensorType.CarbonMonoxide
					|| evt.getSensorType() ==  SensorType.Smoke)
			{
				sendControlPanelMessage("Alarm is trigger by sensor\n" 
						+ evt.getSensorType() + "with ID = " + evt.getSensorID());				
			}
		}
		else if(level == ESecurityLevel.Away)
		{
			if(evt.getSensorAction() == SensorAction.Trigger)
			{
				sendControlPanelMessage("Alarm is trigger by sensor\n" 
					+ evt.getSensorType() + "with ID = " + evt.getSensorID());
			}
		}
		else if(level == ESecurityLevel.Home)
		{
			if(evt.getSensorType() == SensorType.CarbonMonoxide
					|| evt.getSensorType() ==  SensorType.Smoke)
			{
				sendControlPanelMessage("Alarm is trigger by sensor\n" 
						+ evt.getSensorType() + "with ID = " + evt.getSensorID());				
			}
		}
		else if(level == ESecurityLevel.Night)
		{
			if(evt.getSensorAction() == SensorAction.Trigger)
			{
				sendControlPanelMessage("Alarm is trigger by sensor\n" 
					+ evt.getSensorType() + "with ID = " + evt.getSensorID());
			}			
		}

	}
	
	public void controlPanelEventOccurred(ControlPanelEvent evt) 
	{
		if(evt.getReceived() == 0) // display menu
		{
			String menu = "Please choose one of the following settings\n" +
					"1 - Off\n" + "2 - Away\n" + "3 - Home\n" + "4 - Night\n";
			sendControlPanelMessage(menu);
		}
		else if(evt.getReceived() == 1)
		{
			level = ESecurityLevel.Off;
			sendControlPanelMessage("SafeHome system is Off");
		}
		else if(evt.getReceived() == 2)
		{
			level = ESecurityLevel.Away;
			sendControlPanelMessage("SafeHome system is Away");			
		}
		else if(evt.getReceived() == 3)
		{
			level = ESecurityLevel.Home;
			sendControlPanelMessage("SafeHome system is Home");
		}
		else if(evt.getReceived() == 4)
		{
			level = ESecurityLevel.Night;
			sendControlPanelMessage("SafeHome system is Night");
		}
		else if(evt.getReceived() == -2)
		{
			sendControlPanelMessage("Alarm was sent to SafeHome Control :)");
		}
		
	}
	
	public void sendControlPanelMessage(String message)
	{
		fireHandlerEvent(new ServerEvent(this, message));
	}
	
	
	public void addHandlerEventListener(
			IHandlerEventListener eventListener)
	{
		listenerList.add(IHandlerEventListener.class, eventListener);
	}

	public void removeHandlerEventListener(
			IHandlerEventListener eventListener)
	{
		listenerList.remove(IHandlerEventListener.class, eventListener);
	}

	public void removeAllHandlerEventListeners()
	{
		listenerList = new EventListenerList();
	}

	private void fireHandlerEvent(ServerEvent event)
	{
		Object[] listeners = listenerList.getListenerList();
		// Each listener occupies two elements - the first is the listener class
		// and the second is the listener instance
		for (int i = 0; i < listeners.length; i += 2)
		{
			if (listeners[i] == IHandlerEventListener.class)
			{
				((IHandlerEventListener) listeners[i + 1])
						.serverEventOccurred(event);
			}
		}
	}
	
}
